﻿//==============================================================================
// File: AccordionPanel.cs
// Created: 2010-05-28
// Author: Arek Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WpfRcp.Controls
{
    public class AccordionPanel : StackPanel
    {
        private AccordionMode m_AccordionMode = AccordionMode.Single;

        public AccordionPanel()
        {
            AddHandler(Expander.ExpandedEvent, new RoutedEventHandler(SingleExpandedHandler));
        }

        public AccordionMode AccordionMode
        {
            get { return m_AccordionMode; }
            set
            {
                if (m_AccordionMode == AccordionMode.Single)
                {
                    RemoveHandler(Expander.ExpandedEvent, new RoutedEventHandler(SingleExpandedHandler));
                }
                else
                {
                    RemoveHandler(Expander.ExpandedEvent, new RoutedEventHandler(MultiExpandedHandler));
                    RemoveHandler(Expander.CollapsedEvent, new RoutedEventHandler(CollapsedHandler));
                }

                m_AccordionMode = value;

                if (value == AccordionMode.Single)
                {
                    AddHandler(Expander.ExpandedEvent, new RoutedEventHandler(SingleExpandedHandler));
                }
                else
                {
                    AddHandler(Expander.ExpandedEvent, new RoutedEventHandler(MultiExpandedHandler));
                    AddHandler(Expander.CollapsedEvent, new RoutedEventHandler(CollapsedHandler));
                }
            }
        }

        private void MultiExpandedHandler(object sender, RoutedEventArgs e)
        {
            Expander selectedExpander = e.OriginalSource as Expander;
            Expander otherExpander;
            FrameworkElement contentPresenter;
            double totalExpanderHeight = 0.0;
            IList<Expander> expanders = new List<Expander>();

            if (selectedExpander != null)
            {
                foreach (UIElement element in Children)
                {
                    otherExpander = element as Expander;
                    if (otherExpander != null && otherExpander != selectedExpander)
                    {
                        if (otherExpander.IsExpanded)
                        {
                            contentPresenter = otherExpander.Template.FindName("ExpandSite", otherExpander) as FrameworkElement;
                            if (contentPresenter != null)
                                totalExpanderHeight -= contentPresenter.ActualHeight;
                            expanders.Add(otherExpander);
                        }
                        totalExpanderHeight += otherExpander.ActualHeight;
                        totalExpanderHeight += otherExpander.Margin.Top + otherExpander.Margin.Bottom;
                    }
                }

                if (selectedExpander.IsExpanded)
                {
                    int c = expanders.Count == 0 ? 1 : expanders.Count + 1;
                    foreach (var expander in expanders)
                    {
                        contentPresenter = expander.Template.FindName("ExpandSite", expander) as FrameworkElement;

                        if (contentPresenter != null)
                            contentPresenter.Height = (ActualHeight - totalExpanderHeight - selectedExpander.ActualHeight - selectedExpander.Margin.Top - selectedExpander.Margin.Bottom) / c;
                    }

                    contentPresenter = selectedExpander.Template.FindName("ExpandSite", selectedExpander) as FrameworkElement;
                    if (contentPresenter != null)
                        contentPresenter.Height = (ActualHeight - totalExpanderHeight - selectedExpander.ActualHeight - selectedExpander.Margin.Top - selectedExpander.Margin.Bottom) / c;
                }
            }
        }

        private void CollapsedHandler(object sender, RoutedEventArgs e)
        {
            Expander selectedExpander = e.OriginalSource as Expander;
            Expander otherExpander;
            FrameworkElement contentPresenter;
            double totalExpanderHeight = 0.0;
            IList<Expander> expanders = new List<Expander>();

            if (selectedExpander != null)
            {
                foreach (UIElement element in Children)
                {
                    otherExpander = element as Expander;
                    if (otherExpander != null && otherExpander != selectedExpander)
                    {
                        if (otherExpander.IsExpanded)
                        {
                            contentPresenter = otherExpander.Template.FindName("ExpandSite", otherExpander) as FrameworkElement;
                            if (contentPresenter != null)
                                totalExpanderHeight -= contentPresenter.ActualHeight;
                            expanders.Add(otherExpander);
                        }
                        totalExpanderHeight += otherExpander.ActualHeight;
                    }
                }

                if (selectedExpander.IsExpanded == false)
                {
                    contentPresenter = selectedExpander.Template.FindName("ExpandSite", selectedExpander) as FrameworkElement;
                    double hiddenSize = selectedExpander.ActualHeight;
                    if (contentPresenter != null)
                        hiddenSize -= contentPresenter.ActualHeight;
                    int c = expanders.Count == 0 ? 1 : expanders.Count;
                    foreach (var expander in expanders)
                    {
                        contentPresenter = expander.Template.FindName("ExpandSite", expander) as FrameworkElement;

                        if (contentPresenter != null)
                            contentPresenter.Height = (ActualHeight - totalExpanderHeight - hiddenSize) / c;
                    }
                }
            }
        }

        private void SingleExpandedHandler(object sender, RoutedEventArgs e)
        {
            Expander selectedExpander = e.OriginalSource as Expander;
            Expander otherExpander;
            FrameworkElement contentPresenter;
            double totalExpanderHeight = 0.0;

            if (selectedExpander != null)
            {
                foreach (UIElement element in Children)
                {
                    otherExpander = element as Expander;
                    if (otherExpander != null && otherExpander != selectedExpander)
                    {
                        if (otherExpander.IsExpanded)
                        {
                            contentPresenter = otherExpander.Template.FindName("ExpandSite", otherExpander) as FrameworkElement;
                            if (contentPresenter != null)
                                totalExpanderHeight -= contentPresenter.ActualHeight;
                        }
                        otherExpander.IsExpanded = false;
                        totalExpanderHeight += otherExpander.ActualHeight;
                    }
                }

                if (selectedExpander.IsExpanded)
                {
                    contentPresenter = selectedExpander.Template.FindName("ExpandSite", selectedExpander) as FrameworkElement;
                    if (contentPresenter != null)
                        contentPresenter.Height = ActualHeight - totalExpanderHeight - selectedExpander.ActualHeight;
                }
            }
        }
    }

    public enum AccordionMode
    {
        Single,
        Multi
    }
}